/**
 * Copyright (c) 2015-2017, Henry Yang 杨勇 (gismail@foxmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lambkit.plugin.druid;

import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.lambkit.db.DataSourcePlugin;
import com.lambkit.db.datasource.DataSourceConfig;
import com.lambkit.db.datasource.DataSourceFactory;

import java.io.File;

public class DruidDataSourceFactory implements DataSourceFactory {

	@Override
	public DruidDataSourcePlugin createDataSource(DataSourceConfig dataSourceConfig) {
		
		// System.out.println("db_config: " + dataSourceConfig.getName() + ", " +
		// dataSourceConfig.getDbname());
		DruidPlugin druidPlugin = createDruidPlugin(dataSourceConfig);
		String configName = dataSourceConfig.getName();
		DruidDataSourcePlugin druidDataSourcePlugin = new DruidDataSourcePlugin(configName, druidPlugin);
		//druidPlugin.start();
		return druidDataSourcePlugin;
	}

	@Override
	public DataSourcePlugin createDatasourcePlugin(DataSourceConfig dataSourceConfig) {
		// System.out.println("db_config: " + dataSourceConfig.getName() + ", " +
		// dataSourceConfig.getDbname());
		DruidPlugin druidPlugin = createDruidPlugin(dataSourceConfig);
		String configName = dataSourceConfig.getName();
		DruidDataSourcePlugin druidDataSourcePlugin = new DruidDataSourcePlugin(configName, druidPlugin);

		ActiveRecordPlugin activeRecordPlugin = StrUtil.isNotBlank(configName)
				? new ActiveRecordPlugin(configName, druidPlugin)
				: new ActiveRecordPlugin(druidPlugin);

		druidDataSourcePlugin.setArp(activeRecordPlugin);
		return druidDataSourcePlugin;
	}

	private DruidPlugin createDruidPlugin(DataSourceConfig dataSourceConfig) {
		DruidPlugin druidPlugin = new DruidPlugin(dataSourceConfig.getUrl(), dataSourceConfig.getUser(),
				dataSourceConfig.getPassword(), dataSourceConfig.getDriverClassName());
		// 1.统计信息插件
		StatFilter statFilter = new StatFilter();
		statFilter.setMergeSql(dataSourceConfig.isDruidMergeSql());
		statFilter.setLogSlowSql(dataSourceConfig.isDruidLogSlowSql());
		// 慢查询目前设置为1s,随着优化一步步进行慢慢更改
		statFilter.setSlowSqlMillis(dataSourceConfig.getDruidSlowSqlMillis());
		druidPlugin.addFilter(statFilter);
		String dbtype = dataSourceConfig.getType();
		if (!"sqlite".equals(dbtype)) {
			WallFilter wall = new WallFilter();
			String path = PathKit.getRootClassPath() + File.separator + "druid_wall.properties";
			File file = new File(path);
			if (file.exists()) {
				Prop prop = PropKit.use("druid_wall.properties");
				if (prop != null) {
					wall.configFromProperties(prop.getProperties());
				}
			}
			wall.setDbType(dbtype);
			druidPlugin.addFilter(wall);
		}

		druidPlugin.setInitialSize(dataSourceConfig.getInitialSize());
		druidPlugin.setMinIdle(dataSourceConfig.getMinIdle());
		druidPlugin.setMaxActive(dataSourceConfig.getMaxActive());
		druidPlugin.setMaxWait(dataSourceConfig.getMaxWait());
		// 设定数据源名称
		druidPlugin.setName(dataSourceConfig.getName());
		return druidPlugin;
	}

}
